/*
 * Texture.h
 *
 * 	Is an OpenGL-renderable texture.
 * 	Textures can be tossed around at will, PersistantTextures stay on the heap and are freed with clean()
 * 	You shouldn't be using PersistantTextures directly.
 *
 *  Created on: May 5, 2013
 *      Author: zapmunk
 */
#pragma once

class Texture {
private:
	class PersistantTexture {
	private:
		GLuint tex;			//The GL id for this texture
		//GLenum tex_format;	//The GL texture format for this texture	//Is this even necessary?
		GLint tex_colors;	//The GL color format for this texture
		uint width, height;	//The width and height of this texture

		PersistantTexture(const SDL_Surface *const surf) throw(const char*);
		~PersistantTexture();
		friend class Texture;
	};
	PersistantTexture* persistant;

	static const uint NULL_DIMENSION = 0;
	static const GLuint NULL_TEX = 0;
	static const GLint NULL_COLORS = 0;
public:
	//Creates a null texture. Do not attempt to draw/render!
	Texture():
		persistant(NULL),
		w(NULL_DIMENSION), h(NULL_DIMENSION),
		tex(NULL_TEX), tex_colors(NULL_COLORS) {}

	//Creates a new Texture with an associated PersistantTexture. Remember to call clean() once you're through!
	//Potentially expensive!
	Texture(const SDL_Surface *const surf);

	Texture(const Texture& other);
	Texture& operator=(const Texture& other);

	//Returns if this is a valid texture or a null texture
	bool isNull() const {
		return persistant? false : true;
	}

	//Modifies pixel coordinates to GL coordinates
	GLfloat modX(uint x) const {
		if (persistant)
			return x / (float)(persistant->width);
		else
			return 0;
	}
	GLfloat modY(uint y) const {
		if (persistant)
			return y / (float)(persistant->height);
		else
			return 0;
	}

	const uint& w, h;
	const GLuint& tex;
	//const GLenum& tex_format;
	const GLint& tex_colors;

	//Cleans up the PersistantTexture associated with this texture
	//CAUTION: do not attempt to use a texture or any derived textures that have been cleaned!
	void clean();
};
